/**
 * 
 */
package cn.edu.security.core.social.wx.config;

import cn.edu.security.core.properties.SecurityProperties;
import cn.edu.security.core.properties.WeiXinProperties;
import cn.edu.security.core.social.EduConnectView;
import cn.edu.security.core.social.wx.connect.WeiXinConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import cn.edu.security.core.social.configutils.SocialAutoConfigurerAdapter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.web.servlet.View;

/**
 * 微信登录配置
 *
 */
@Configuration
@ConditionalOnProperty(prefix = "edu.security.social.weixin", name = "appId")
public class WeiXinAutoConfiguration extends SocialAutoConfigurerAdapter {

	@Autowired
	private SecurityProperties securityProperties;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.boot.autoconfigure.social.SocialAutoConfigurerAdapter
	 * #createConnectionFactory()
	 */
	@Override
	protected ConnectionFactory<?> createConnectionFactory() {
		WeiXinProperties weiXinConfig = securityProperties.getSocial().getWeixin();
		return new WeiXinConnectionFactory(weiXinConfig.getProviderId(), weiXinConfig.getAppId(),
				weiXinConfig.getAppSecret());
	}
	
	@Bean({"connect/weiXinConnect", "connect/weiXinConnected"})
	@ConditionalOnMissingBean(name = "weiXinConnectedView")
	public View weiXinConnectedView() {
		return new EduConnectView();
	}
	
}
